package com.infoland.afanmall.model.shopCart.adapter;//package com.infoland.afanmall.model.shopCart.adapter;
//

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.DecimalKit;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.shopCart.activity.ShopCartActivity;
import com.infoland.afanmall.model.shopCart.entity.MyViewHolder;
import com.infoland.afanmall.model.shopCart.linster.CartCountLinster;
import com.infoland.afanmall.model.shopCart.linster.CartCheckLinster;
import com.infoland.afanmall.model.shopCart.linster.CartDeleteLinster;

import java.math.BigDecimal;
import java.util.ArrayList;

//
public class CartAdapter extends RecyclerSwipeAdapter<MyViewHolder> {
    CheckBox checkBox_A1;
    CheckBox checkBox_A2;
    ShopCartActivity shopCartActivity;

    public CartAdapter(CheckBox checkBox_A1, CheckBox checkBox_A2, ShopCartActivity shopCartActivity) {
        this.checkBox_A1 = checkBox_A1;
        this.checkBox_A2 = checkBox_A2;
        this.shopCartActivity = shopCartActivity;
    }

    /****创建ViewHoler(在 MyViewHolder 构造中初始化控件)**/
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_goods_item, parent, false);
//        return new MyViewHolder(view);
        return new MyViewHolder(view);
    }

    /****调用控件**/
    @Override
    public void onBindViewHolder(MyViewHolder viewHolder, int position) {
        ShopCartActivity.checkBoxList.add(viewHolder.checkBox);
        ShopCartActivity.textViewList1.add(viewHolder.cartGoodsPrice);
        ShopCartActivity.textViewList2.add(viewHolder.cart_goods_price2);
//        swipLinster(viewHolder);
        ToolsKit.setFocusShow(viewHolder.cartGoodsCount);
        ToolsKit.setBackImg(ShopCartActivity.cartListBeans.get(position).getF_url() + "", viewHolder.cartGoodsImg);//商品图片
        int goods_count = ShopCartActivity.cartListBeans.get(position).getGoods_count();
        viewHolder.cartGoodsCount.setText((goods_count + ""));//获取商品数量
        viewHolder.cartGoodsName.setText((ShopCartActivity.cartListBeans.get(position).getGoods_name() + ""));//商品名称
        viewHolder.cartGoodsSv.setText((ShopCartActivity.cartListBeans.get(position).getSv() + ""));//商品规格
        double goods_price = ShopCartActivity.cartListBeans.get(position).getGoods_price();
        BigDecimal mul = DecimalKit.mul(goods_count, goods_price);
        viewHolder.cartGoodsPrice.setText(("￥" + mul + ""));//商品单价
        viewHolder.cart_goods_price2.setText(("￥" + mul + ""));//商品单价
        /****商品详情  图片跳转*/
        viewHolder.cartGoodsImg.setOnClickListener(v -> {
        });
        /****商品详情  文字跳转*/
        viewHolder.cartGoodsName.setOnClickListener(v -> {
        });

        /***单个CheckBox点击事件*/
        viewHolder.checkBoxLayout.setOnClickListener(v -> {
            boolean checked = viewHolder.checkBox.isChecked();
            if (checked) {
                viewHolder.checkBox.setChecked(false);
            } else {
                viewHolder.checkBox.setChecked(true);
            }
            updateCheckAll();
            cartCheckLinster.updateView();
        });

        /***全选、反选*/
        checkBox_A1.setOnClickListener(v -> {
            if (checkBox_A1.isChecked()) {
                ckechedAll();
            } else {
                deleAll();
            }
            /***通知改变*/
            cartCheckLinster.updateView();
        });
        /***全选、反选*/
        checkBox_A2.setOnClickListener(v -> {
            if (checkBox_A2.isChecked()) {
                ckechedAll();
            } else {
                deleAll();
            }
            cartCheckLinster.updateView();
        });

        //减少数量
        viewHolder.cartSubtraction.setOnClickListener(v -> {
            cartCountLinster.update(1, position, viewHolder.cartGoodsCount, viewHolder.cartGoodsPrice, viewHolder.cart_goods_price2);
        });

        //添加数量
        viewHolder.cartAddition.setOnClickListener(v -> {
            cartCountLinster.update(2, position, viewHolder.cartGoodsCount, viewHolder.cartGoodsPrice, viewHolder.cart_goods_price2);

        });

        /****单个Item删除*/
        viewHolder.bottom_delete_bt.setOnClickListener(v -> {
            LogKit.E(" 单个Item删除  ", " 单个Item删除 ");
            cartDeleteLinster.deleteOption(ShopCartActivity.cartListBeans.get(position).getId());
        });

    }

    /****更新状态是否全选*/
    private void updateCheckAll() {
        int allSize = ShopCartActivity.checkBoxList.size();
        ArrayList<Object> objects = new ArrayList<>();
        for (CheckBox c : ShopCartActivity.checkBoxList) {
            if (c.isChecked()) {
                objects.add("");
            }
        }
        if (objects.size() == allSize && allSize != 0) {
            checkBox_A1.setChecked(true);
            checkBox_A2.setChecked(true);
        } else {
            checkBox_A1.setChecked(false);
            checkBox_A2.setChecked(false);
        }
    }

    /***反选*/
    private void deleAll() {
        for (CheckBox c : ShopCartActivity.checkBoxList) {
            c.setChecked(false);
        }
    }

    /***全选*/
    private void ckechedAll() {
        for (CheckBox c : ShopCartActivity.checkBoxList) {
            c.setChecked(true);
        }
    }


    /***返回数据源长度**/
    @Override
    public int getItemCount() {
        return ShopCartActivity.cartListBeans.size();
    }

    /***返回刷新布局的ID**/
    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.itemview_swipe;
    }

    private CartCheckLinster cartCheckLinster;

    public void setCartCheckLinster(CartCheckLinster cartCheckLinster) {
        this.cartCheckLinster = cartCheckLinster;
    }

    private CartCountLinster cartCountLinster;

    public void setCartCountLinster(CartCountLinster cartCountLinster) {
        this.cartCountLinster = cartCountLinster;
    }

    private CartDeleteLinster cartDeleteLinster;

    public void setCartDeleteLinster(CartDeleteLinster cartDeleteLinster) {
        this.cartDeleteLinster = cartDeleteLinster;
    }

    private void swipLinster(MyViewHolder viewHolder) {
//        viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
//            @Override
//            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
////                LogKit.E("   onHandRelease", " onHandRelease ");
////                if (viewHolder.cartGoodsCount.isFocusable()) {
////
////                } else {
////
////                }
//
//            }
//
//            @Override
//            public void onClose(SwipeLayout layout) {
////                LogKit.E("   onClose", "  onClose");
//            }
//
//            @Override
//            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
////                LogKit.E("   onUpdate", "  onUpdate");
//
//            }
//
//            @Override
//            public void onStartOpen(SwipeLayout layout) {
////                LogKit.E("   onStartOpen", " onStartOpen ");
//
//            }
//
//            @Override
//            public void onOpen(SwipeLayout layout) {
////                LogKit.E("   onOpen", "  onOpen");
//            }
//
//            @Override
//            public void onStartClose(SwipeLayout layout) {
////                LogKit.E("   onStartClose", " onStartClose ");
//
//            }
//
//
//        });
    }
}